package com.tz.hotelmanager.controller;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.hotelmanager.domain.User;
import com.tz.hotelmanager.domain.UserExample;
import com.tz.hotelmanager.domain.status;
import com.tz.hotelmanager.mapper.impl.UserMapperImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.tz.hotelmanager.Utils.*;

@RestController
public class UserController {

  @Autowired
  UserMapperImpl userMapperImpl;

  @PostMapping(value = "/altUser")
  public String altUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    result.put("req", JSONObject.parse(jsonparm.toJSONString()));
    if (session.getAttribute("userbase") == null) {
      session.setAttribute("userbase", null);
      return null;
    }
    if (jsonparm == null || jsonparm.getString("id")  == null || jsonparm.getString("name") == null) {
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    JSONObject userbase = JSONObject.parseObject((String) session.getAttribute("userbase"));
    if(userbase.getString("type").equals("1"))
    {
      UserExample userExample = new UserExample();
      UserExample.Criteria  uCriteria = userExample.createCriteria();
      uCriteria.andNameEqualTo(jsonparm.getString("name"));
      User record = new User();
      record.setAddress(jsonparm.getString("address"));
      record.setPhone(jsonparm.getString("phone"));
      record.setInfo(jsonparm.getString("info"));
      record.setPassword(jsonparm.getString("password"));
      userMapperImpl.updateByExampleSelective(record, userExample);
      result.put("result", status.s_ok);
      User user = userMapperImpl.selectByExample(userExample).get(0);
      result.put("data", JSONObject.parse(user.toString()));
      return result.toJSONString();
    }else{
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    
    // JSONObject jsonObject =
    // if(session.getAttribute("userbase") == null ){
    // session.setAttribute("userbase", null);
    // return null ;
    // }
    // return entity;
  }

  @PostMapping(value = "/deluser")
  public String delUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    if (session.getAttribute("userbase") == null) {
      session.setAttribute("userbase", null);
      return null;
    }
    if (jsonparm == null || jsonparm.getString("name") == null) {
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    JSONObject userbase = JSONObject.parseObject((String) session.getAttribute("userbase"));
    if(userbase.get("type").equals("1"))
    {
      UserExample userExample = new UserExample();
      UserExample.Criteria  uCriteria = userExample.createCriteria();
      uCriteria.andNameEqualTo(jsonparm.getString("name"));
      userMapperImpl.deleteByExample(userExample);
      result.put("result",status.s_ok);
      return result.toJSONString();
    }else{
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    
    // JSONObject jsonObject =
    // if(session.getAttribute("userbase") == null ){
    // session.setAttribute("userbase", null);
    // return null ;
    // }
    // return entity;
  }

  @PostMapping(value = "/adduser")
  public String adduser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    if (session.getAttribute("userbase") == null) {
      session.setAttribute("userbase", null);
      return null;
    }
    if (jsonparm == null) {
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    JSONObject userbase = JSONObject.parseObject((String) session.getAttribute("userbase"));
    if(userbase.get("type").equals("1"))
    {
      UserExample userExample = new UserExample();
      UserExample.Criteria criteria=  userExample.createCriteria();
      criteria.andNameEqualTo(jsonparm.getString("name"));
      List<User> checklist=  userMapperImpl.selectByExample(userExample);
      if(checklist == null ||  checklist.size() > 0) {
         result.put("result", status.s_error);
         return result.toJSONString();
      }
      User user =new User();
      user.setAddress((String)jsonparm.getString("address"));
      user.setPhone((String)jsonparm.getString("phone"));
      user.setPassword((String)jsonparm.getString("password"));
      user.setInfo((String)jsonparm.getString("info"));
      user.setName((String)jsonparm.getString("name"));
      user.setType("2");
      user.setCreatetime( new Date());
      user.setIncomes(0d);
      userMapperImpl.insert(user);
      result.put("result", status.s_ok);
      return result.toJSONString();
    }else{
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    
    // JSONObject jsonObject =
    // if(session.getAttribute("userbase") == null ){
    // session.setAttribute("userbase", null);
    // return null ;
    // }
    // return entity;
  }

  @PostMapping(value = "/getUser")
  public String getUser(HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    if (session.getAttribute("userbase") == null) {
      session.setAttribute("userbase", null);
      return result.toJSONString();
    }
    JSONObject jsonObject = JSONObject.parseObject((String) session.getAttribute("userbase"));
    if (!jsonObject.get("type").equals("1")) {
      result.put("type",jsonObject.get("type") );
      return result.toJSONString();
    }
     
    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andTypeEqualTo("2");
    List<User> users = userMapperImpl.selectByExample(userExample);
    JSONArray jsonArray = new JSONArray();
    if (users.size() == 0) {
      result.put("result", status.s_ok);
      result.put("data", "{}");
      return result.toJSONString();
    }
    for (User user : users) {
      JSONObject obj = JSONObject.parseObject(user.toString());
      jsonArray.add(obj);
    }
    return jsonArray.toJSONString();
    // JSONObject jsonObject =
    // if(session.getAttribute("userbase") == null ){
    // session.setAttribute("userbase", null);
    // return null ;
    // }
    // return entity;
  }

  @PostMapping(value = "/regUser")
  public String regUser(@RequestBody JSONObject jsonparm,HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
     
    if (jsonparm == null) {
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    {
       UserExample userExample = new UserExample();
       UserExample.Criteria criteria=  userExample.createCriteria();
       criteria.andNameEqualTo(jsonparm.getString("name"));
       List<User> checklist=  userMapperImpl.selectByExample(userExample);
       if(checklist == null ||  checklist.size() > 0) {
          result.put("result", status.s_ok);
          return result.toJSONString();
       }
    }
    User user = new User();
    user.setAddress(jsonparm.getString("address"));
    user.setInfo(jsonparm.getString("email"));
    user.setPassword(jsonparm.getString("pass"));
    user.setName(jsonparm.getString("name"));
    user.setPhone(jsonparm.getString("phone"));
    user.setCreatetime(new Date());
    user.setIncomes(0d);
    user.setType("3");
    userMapperImpl.insert(user);
    result.put("result", status.s_ok);
    return result.toJSONString();
 
  }

 }